﻿// ==PREPROCESSOR==
// @name "Mini Panel"
// @author "keperlia"
// @version "1.01"
// @feature "dragdrop"
// ==/PREPROCESSOR==

ThisPanelsName ="MiniPanel";
var hWnd = utils.CreateWND(window.ID).GetAncestor(1);
var icoPath = fb.ProfilePath + "skins\\icons\\";
var ww = wh = mid = 0;
var shadow = null;
var move = click = false;
var _x = _y = x_ = y_ = 0;
var g_img = bg_img = null;
var blurT = blurB = blurV = null;
var bg_x = bg_y = bg_w = bg_h = 0;
var g_font = gdi.Font("Segoe UI",13);
var title = artist = null;
var metadb = null;
var seek = null;
var volume = {bar:null, visable:false};
var g_fade = 0;
var g_timer = null;
var max = window.GetProperty('大封面', true);
var blurcolor = 0;
var DT_CV = 0x00000001|0x00000004|0x00000400|0x00008000;
var DT_LV = 0x00000000|0x00000004|0x00000400|0x00008000;
PBO = ['Default','Repeat (playlist)','Repeat (track)','Random','Shuffle (tracks)','Shuffle (albums)','Shuffle (folders)'];
// =====================button========================
var btn_h_img = btn_d_img = null;
var close = hide = expand = null;
var prev_img = gdi.Image(icoPath + "prev.png");
var play_img = gdi.Image(icoPath + "play.png");
var pause_img = gdi.Image(icoPath + "pause.png");
var next_img = gdi.Image(icoPath + "next.png");
var pbo_img = gdi.Image(icoPath + 'Pbo\\' + fb.PlaybackOrder + '.png');
var vol_img = gdi.Image(icoPath + 'Vol\\1.png');
var lrc_img = gdi.Image(icoPath + "focus.png");
var btn1 = btn2 = btn3 = btn4 = btn5 = btn6 = btn7 = null;

oButton = function(Image){
	this.state = 0; //0-normal ;1-hover ;2-down
	this.img = Image;
	this.fade = 128;

	this.repaint = function(){
		window.RepaintRect(this.x,this.y,this.w,this.h);
	}

	this.Locate = function(x,y,w,h){
		this.x = x;
		this.y = y;
		this.w = w;
		this.h = h;
	}

	this.Draw = function(gr){
		this.img&&gr.DrawImage(this.img,this.x,this.y,this.w,this.h,0,0,this.img.Width,this.img.Height,0,this.fade);
	}

	this.Activate = function(_x,_y){
		if(_x > this.x && _x <(this.x + this.w) && _y > this.y && _y <(this.y + this.h)){
			if(this.state == 0){
				this.fade = 255;
				this.state = 1;
				window.RepaintRect(this.x,this.y,this.w,this.h);
				return true;
			}
		}
		else this.Reset();
	}

	this.Down = function(){
		if(this.state == 1){
			this.fade = 192;
			this.state = 2;
			window.RepaintRect(this.x,this.y,this.w,this.h);
			return true;
		}
		else return false;
	}

	this.Reset = function(){
		if(this.state != 0){
			this.fade = 128;
			this.state = 0;
			window.RepaintRect(this.x,this.y,this.w,this.h);
			return true;
		}
	}
}

oButtonV2 = function(X, Y, W, H, Image,ImageHover,ImageDown){
	this.state = 0; //0-normal ;1-hover ;2-down
	this.image = Image;
	this.img = null;

	var timer = null;
	var fade = 255;
	var x = X;
	var y = Y;
	var w = W;
	var h = H;

	this.repaint = function(){
		window.RepaintRect(x,y,w,h);
	}

	this.Locate = function(_x,_y,_w,_h){
		x = _x;
		y = _y;
		w = _w;
		h = _h;
	}

	this.Draw = function(gr){
		this.img&&gr.DrawImage(this.img,x,y,w,h,0,0,this.img.Width,this.img.Height,0,fade);
		this.image&&gr.DrawImage(this.image,x,y,w,h,0,0,this.image.Width,this.image.Height,0,255);
	}

	this.Activate = function(_x,_y){
		if(_x > x && _x <(x + w) && _y > y && _y <(y + h)){
			if(this.state == 0){
				this.img = ImageHover;
				this.state = 1;
				fade = 55;
				timer&&window.ClearInterval(timer);
				timer = window.SetInterval(function(){
					fade += 40;
					if(fade >= 255){
						window.ClearInterval(timer);
						timer = null;
					}
					window.RepaintRect(x,y,w,h);
				},50);
				return true;
			}
		}
		else this.Reset();
	}

	this.Down = function(){
		if(this.state == 1){
			this.img = ImageDown;
			this.state = 2;
			window.RepaintRect(x,y,w,h);
			return true;
		}
		else return false;
	}

	this.Reset = function(){
		if(this.state != 0){
			this.state = 0;
			fade = 200;
			timer&&window.ClearInterval(timer);
			timer = window.SetInterval(function(){
				fade -= 40;
				if(fade <= 0){
					this.img = null;
					window.ClearInterval(timer);
					timer = null;
				}
				window.RepaintRect(x,y,w,h);
			},50);
			return true;
		}
	}
}

oDrag = function(type){
	this.type = type;
	this.Pos = 0;
	this.X = 0;
	this.Y = 0;
	this.W = 0;
	this.H = 0;

	var xx,yy;
	var r = this.X + this.W;
	var b = this.Y + this.H;
	var dragging = false;

	this.repaint = function(reset){
		window.RepaintRect(xx-1,yy-1,this.W+2,this.H+2);
	}

	this.Locate = function(_x,_y,_w,_h){
		xx = this.X = _x;yy = this.Y = _y;this.W = _w;this.H = _h;
		r = _x + _w;b = _y + _h;
	}

	this.Click = function(_x,_y){
		if(_x>this.X && _x<r && _y>this.Y && _y<b){
			dragging = true;
			if('horizon' == this.type)
			{
				this.Pos = _x - this.X;
			}
			else {
				this.Pos = _y - this.Y;
			}
			window.RepaintRect(xx,yy,this.W,this.H);
			return true;
		}
		else return false;
	}

	this.Drag = function(_x,_y){
		if(dragging){
			if('horizon' == this.type){
				if(_x > this.X){
					this.Pos = _x - this.X;
					if(this.Pos > this.W){
						this.Pos = this.W;
					}
				}else{
					this.Pos = 0;
				}
			}
			else{
				if(_y > this.Y){
					this.Pos = _y - this.Y;
					if(this.Pos > this.H){
						this.Pos = this.H;
					}
				}else{
					this.Pos = 0;
				}
			}
			window.RepaintRect(xx,yy,this.W,this.H);
			return true;
		}
		else return false;
	}

	this.Reset = function(){
		if(dragging){
			dragging = false;
			window.RepaintRect(xx,yy,this.W,this.H);
		}
	}
}

oTooltip = function(){
	var _x_ = _y_ = _w_ = _h_ = 0;
	var timer = null;
	var timeout = null;
	var visable = false;
	var img = null;
	var fade = 0;
	var tip = '';

	this.Locate = function(x, y, w, h){
		_x_ = x;
		_y_ = y;
		_w_ = w;
		_h_ = h;
	}

	function Draw(){
		img = gdi.CreateImage(_w_, _h_);
		var gb = img.GetGraphics();
		gb.SetSmoothingMode(2);
		gb.FillSolidRect(0, 0, _w_ - 1, _h_ - 1, RGB(64,64,64));
		gb.DrawRect(0, 0, _w_ - 1, _h_ - 1, 1, RGB(32,32,32));
		img.ReleaseGraphics(gb);
		img = imagePro(img, _w_, _h_);
	}

	function Dispose(){
		fade = 200;
		timer = window.SetInterval(function(){
			fade -= 20;
			if(fade == 0){
				visable = false;
				img.Dispose();
				img = null;
				timer&&window.ClearInterval(timer);
				timer = null;
			}
			window.RepaintRect(_x_, _y_, _w_, _h_);
		},17);
	}

	this.Show = function(text){
		if(visable){
			timeout&&window.ClearTimeout(timeout);
			timeout = null;
			timer&&window.ClearInterval(timer);
			timer = null;
			img&&img.Dispose();
			img = null;
			window.RepaintRect(_x_, _y_, _w_, _h_);
			fade = 255;
			tip = text;
			_w_ = Math.floor(calc(text, g_font, true) + 50);
			_x_ = Math.floor((ww - _w_)/2);
			Draw();
			window.RepaintRect(_x_, _y_, _w_, _h_);
			timeout = window.SetTimeout(function(){
				Dispose();
				timeout&&window.ClearTimeout(timeout);
				timeout = null;
			},2000);
		}else{
			visable = true;
			fade = 55;
			tip = text;
			_w_ = Math.floor(calc(text, g_font, true) + 50);
			_x_ = Math.floor((ww - _w_)/2);
			Draw();
			timer = window.SetInterval(function(){
				fade += 20;
				if(fade == 255){
					window.ClearInterval(timer);
					timer = null;
					timeout = window.SetTimeout(function(){
						Dispose();
					},2000);
				}
				window.RepaintRect(_x_, _y_, _w_, _h_);
			},17);
		}
	}

	this.Paint = function(gr){
		if(visable){
			gr.SetSmoothingMode(2);
			gr.DrawImage(img, _x_, _y_, _w_, _h_, 0, 0, _w_, _h_, 0, fade);
			gr.GdiDrawText(tip, g_font, RGB(255,255,255), _x_, _y_, _w_, _h_, DT_CV);
			gr.SetSmoothingMode(0);
		}
	}
}

String.prototype.validate = function(){
	return this.replace(/[\/\\|:]/g, '-').replace(/\*/g, 'x').replace(/"/g, "''").replace(/[<>]/g, '_').replace(/\?/g, "");
}

function Menu(x,y){
	var c = fb.CreateContextMenuManager();
	var nowplaying = fb.GetNowPlaying();
	if(!nowplaying){ return; }
	c.InitContext(nowplaying);
	var a = window.CreatePopupMenu();
	var idx;
	c.BuildMenu(a,1,-1);
	idx = a.TrackPopupMenu(x,y);

	switch(true){
	default:
		c.ExecuteByID(idx - 1);
		break;
	}
	c.Dispose();
	a.Dispose();
}

function getAlbumArt(metadb, state, embedded){
	var embed = (typeof(embedded) == 'undefined')?false:embedded;
	var img1 = img2 = null;
	if(embed){
		img1 = utils.GetAlbumArtEmbedded(metadb.RawPath, state);
	}
	if(img1 == null){
		img1 = utils.GetAlbumArtV2(metadb, state);
	}
	switch(state){
	case 0:
		img2 = gdi.Image(fb.ProfilePath + 'covers\\albums\\' + fb.TitleFormat("[%album%]").EvalWithMetadb(metadb).validate()+'.jpg');
		break;
	case 4:
		img2 = gdi.Image(fb.ProfilePath + 'covers\\artists\\' + fb.TitleFormat("[%artist%]").EvalWithMetadb(metadb).validate()+'.jpg');
		break;
	default: break;
	}
	if(img1 == null){
		return img2;
	}else if(img2 == null){
		return img1;
	}else if(embed){
		return img1;
	}else{
		return (img1.Width*img1.Height > img2.Width*img2.Height)?img1:img2;
	}
}

function getImage(){
	if(!ww||!wh){return;}
	if(g_img){
		g_img.Dispose();
		bg_img.Dispose();
		g_img = null;
		bg_img = null;
	}
	var t_metadb = null;
	if(fb.IsPlaying||fb.IsPaused){t_metadb = fb.GetNowPlaying();}
	if(t_metadb){
		g_img = getAlbumArt(t_metadb,0);
		if(!g_img){
			g_img = getAlbumArt(t_metadb,4);
		}
	}
	if(!g_img){
		if(t_metadb&&t_metadb.Length == 0){
			g_img = gdi.Image(fb.ProfilePath+"skins\\images\\nostream.jpg");
		}else{
			g_img = gdi.Image(fb.ProfilePath+"skins\\images\\nocover.jpg");
		}
	}
	if(g_img){
		var gb = null;
		bg_img = gdi.CreateImage(ww-10,wh-10);
		gb = bg_img.GetGraphics();
		var img_scale = Math.max((ww  - 10)/ g_img.Width, (wh - 10) / g_img.Height);
		bg_w = Math.floor(g_img.Width * img_scale);
		bg_h = Math.floor(g_img.Height * img_scale);
		bg_x = Math.floor((ww - 10 - bg_w) / 2);
		bg_y = Math.floor((wh - 10 - bg_h) / 2);
		gb.DrawImage(g_img,bg_x,bg_y,bg_w,bg_h,0,0,g_img.Width,g_img.Height,0,255);
		gb.FillSolidRect(0, 0, ww - 10, wh - 10, blurcolor);
		bg_img.ReleaseGraphics(gb);
		bg_img.BoxBlur(25,2);

		if(!max){
			img_scale = Math.min((wh - 20) / g_img.Width, (wh - 20) / g_img.Height);
			bg_w = Math.floor(g_img.Width * img_scale);
			bg_h = Math.floor(g_img.Height * img_scale);
			bg_x = Math.floor(((wh - 20) - bg_w) / 2 + 25);
			bg_y = Math.floor((wh - bg_h) / 2);
			g_img = imagePro(g_img,bg_w,bg_h);
		}else{
			img_scale = Math.min((wh - 10) / g_img.Width, (wh - 10) / g_img.Height);
			bg_w = Math.floor(g_img.Width * img_scale);
			bg_h = Math.floor(g_img.Height * img_scale);
			bg_x = Math.floor(((wh - 10) - bg_w) / 2 + 5);
			bg_y = Math.floor((wh - bg_h) / 2);
			g_img = g_img.Resize(bg_w,bg_h,2);
		}
	}
}

function getProgress(){
	var p = fb.PlaybackTime/fb.PlaybackLength;
	return p;
}

function setProgress(p){
	fb.PlaybackTime = fb.PlaybackLength * p;
}

function setVolume(pos){
	fb.Volume =(50 * Math.log(0.99 * pos + 0.01) / Math.LN10);
}

function getVolume(){
	var p =((Math.pow(10, fb.Volume / 50) - 0.01) / 0.99);
	return p;
}

function getText(){
	if(fb.IsPaused||fb.IsPlaying){
		metadb = fb.GetNowPlaying();
	}
	if(metadb){
		artist = fb.TitleFormat("[%artist%]").EvalWithMetadb(metadb);
		title = fb.TitleFormat("[%title%]").EvalWithMetadb(metadb);
	}
}
getText();

function getOverlayV2(l,t,r,b,w,h,color1,color2){
	var temp = gdi.CreateImage(w,h);
	var gb = temp.GetGraphics();
	gb.FillSolidRect(0,0,w, h, color1);
	gb.FillSolidRect(l, t, w - l - r, h - t - b, color2);
	temp.ReleaseGraphics(gb);
	return temp;
}

function RGBA(r, g, b, a){
	return ((a << 24) | (r << 16) | (g << 8) | (b));
}

function RGB(r, g, b){
	return (0xff000000 | (r << 16) | (g << 8) | (b));
}

function getIcons(){
	var gb = null;

	btn_h_img = gdi.CreateImage(30,26);
	gb = btn_h_img.GetGraphics();
	gb.FillSolidRect(1, 1, 29, 25, RGBA(225,225,225,100));
	gb.DrawRect(0, 0, 29, 25, 1, RGB(192,192,192));
	btn_h_img.ReleaseGraphics(gb);

	btn_d_img = gdi.CreateImage(30,26);
	gb = btn_d_img.GetGraphics();
	gb.FillSolidRect(1, 1, 29, 25, RGB(64,64,64));
	gb.DrawRect(0, 0, 29, 25, 1, RGB(128,128,128));
	btn_d_img.ReleaseGraphics(gb);

	close = gdi.CreateImage(16,16);
	gb = close.GetGraphics();
	gb.SetSmoothingMode(2);
	gb.FillEllipse(1, 1, 14, 14, RGBA(0,0,0,192));
	gb.DrawLine(5, 5, 11, 11, 2, 0xffffffff);
	gb.DrawLine(5, 11, 11, 5, 2, 0xffffffff);
	close.ReleaseGraphics(gb);

	hide = gdi.CreateImage(16,20);
	gb = hide.GetGraphics();
	gb.SetSmoothingMode(2);
	gb.FillRoundRect(2, 2, 12, 16, 2, 2, RGBA(0,0,0,192));
	gb.FillPolygon(0xffffffff, 0, Array(4,4, 12,4, 8,9));
	gb.FillPolygon(0xffffffff, 0, Array(4,16, 12,16, 8,11));
	hide.ReleaseGraphics(gb);

	expand = gdi.CreateImage(16,20);
	gb = expand.GetGraphics();
	gb.SetSmoothingMode(2);
	gb.FillRoundRect(2, 2, 12, 16, 2, 2, RGBA(0,0,0,192));
	gb.FillPolygon(0xffffffff, 0, Array(4,9, 12,9, 8,4));
	gb.FillPolygon(0xffffffff, 0, Array(4,11, 12,11, 8,16));
	expand.ReleaseGraphics(gb);
}
getIcons();

function calc(str,font,GDI){
	var temp_bmp = gdi.CreateImage(1, 1);
	var temp_gr = temp_bmp.GetGraphics();
	if(GDI){
		var width = temp_gr.CalcTextWidth(str, font);  //GDI
		temp_bmp.ReleaseGraphics(temp_gr);
		temp_bmp.Dispose();
		temp_gr = null;
		temp_bmp = null;
		return width;
	}else{
		var info = temp_gr.MeasureString(str, font, 0, 0, 99999, 99999, 0);  //GDI+
		temp_bmp.ReleaseGraphics(temp_gr);
		temp_bmp.Dispose();
		temp_gr = null;
		temp_bmp = null;
		return info.Width;
	}
}

function imagePro(img,w,h){
	var image = img.Resize(w - 6, h - 6, 2);
	var gb = null;

	var shadow = gdi.CreateImage(w, h);
	gb = shadow.GetGraphics();
	gb.SetSmoothingMode(2);
	gb.FillRoundRect(3, 3, w - 6, h - 6, 2, 2, RGBA(0,0,0,192));
	shadow.ReleaseGraphics(gb);
	shadow.BoxBlur(2,1);

	var temp = gdi.CreateImage(w, h);
	gb = temp.GetGraphics();
	gb.SetSmoothingMode(2);
	gb.SetInterpolationMode(5);
	gb.DrawImage(shadow, 0, 0, w, h, 0, 0, w, h, 0, 225);
	gb.DrawImage(image, 3, 3, w - 6, h - 6, 0, 0, w - 6, h - 6, 0, 255);
	gb.DrawRect(3, 3, w - 6, h - 6, 1, RGBA(0,0,0,64));
	temp.ReleaseGraphics(gb);

	image.Dispose();
	shadow.Dispose();
	return temp;
}

seek = new oDrag('horizon');
volume.bar = new oDrag('vertical');
tooltip = new oTooltip();
btn0 = new oButtonV2(0, 0, 0, 0, prev_img,btn_h_img,btn_d_img);
btn1 = new oButtonV2(0, 0, 0, 0, fb.IsPlaying?pause_img:play_img,btn_h_img,btn_d_img);
btn2 = new oButtonV2(0, 0, 0, 0, next_img,btn_h_img,btn_d_img);
btn3 = new oButton(close);
btn5 = new oButtonV2(0, 0, 0, 0, pbo_img,btn_h_img,btn_d_img);
btn6 = new oButtonV2(0, 0, 0, 0, lrc_img,btn_h_img,btn_d_img);
btn7 = new oButtonV2(0, 0, 0, 0, vol_img,btn_h_img,btn_d_img);

function setSize(w,h){
	hWnd.Width = w;
	hWnd.Height = h;
	if(utils.IsVistaOrGreater()&&utils.IsAeroEnabled()){
		hWnd.CreateGlass(5,5,5,5,0);
		shadow = getOverlayV2(5,5,5,5,w,h,0,0xff000000);
		shadow.BoxBlur(2,2);
	}
}
max?setSize(225,225):setSize(280,100);

function on_size(){
	ww = window.Width;
	wh = window.Height;
	mid = Math.floor(ww/2);

	ImageAnime();
	btn3.Locate(6,7,16,16);

	if(!max){
		seek.Locate(120,70,ww-165,10);
		btn0.Locate(120,22,30,26);
		btn1.Locate(160,17,36,36);
		btn2.Locate(206,22,30,26);
		btn4 = new oButton(expand);
		btn4.Locate(6,wh - 30, 16, 20);
		btn5.Locate(240, 22, 30, 26);
	}else{
		seek.Locate(40,wh-20,ww-80,10);
		volume.bar.Locate(ww - 28, 60, 5, 100);
		volume.bar.Pos = parseInt(getVolume() * volume.bar.H);
		switch(true){
		case volume.bar.Pos <= 0:
			btn7.image = gdi.Image(icoPath + 'Vol\\1.png');
			break;
		case volume.bar.Pos <= 30:
			btn7.image = gdi.Image(icoPath + 'Vol\\2.png');
			break;
		case volume.bar.Pos <= 60:
			btn7.image = gdi.Image(icoPath + 'Vol\\3.png');
			break;
		case volume.bar.Pos <= 100:
			btn7.image = gdi.Image(icoPath + 'Vol\\4.png');
			break;
		default: break; 
		}
		tooltip.Locate(mid - 50, wh - 100, 100, 35);
		btn0.Locate(mid - 60,wh - 53,30,26);
		btn1.Locate(mid - 18,wh - 57,36,36);
		btn2.Locate(mid + 30,wh - 53,30,26);
		btn4 = new oButton(hide);
		btn4.Locate(6, 30, 16, 20);
		btn5.Locate(mid + 72,wh - 53,30,26);
		btn6.Locate(mid - 102,wh - 53,30,26);
		btn7.Locate(ww - 40,17,30,26);
	}
}

function on_playback_starting(cmd, is_paused){
	btn1.image = pause_img;
	btn1.repaint();
}

function on_playback_new_track(metadb){
	getText();
	ImageAnime();
}

function on_playback_pause(state){
	if(state){
		btn1.image = play_img;
	}else{
		btn1.image = pause_img;
	}
	btn1.repaint();
}

function on_playback_stop(reason){
	if(reason != 2){    
		btn1.image = play_img;
		btn1.repaint();
		seek.Pos = 0;
		ImageAnime();
	}
}

function on_mouse_lbtn_down(x,y){
	if(btn0.Down()){
		fb.Prev();
		btn0.Reset();
	}else if(btn1.Down()){
		fb.PlayOrPause();
		btn1.Reset();
	}
	else if(btn2.Down()){
		fb.Next();
		btn2.Reset();
	}
	else if(btn3.Down()){
		window.NotifyOthers("MainPanel", "MainMode");
		btn3.Reset();
	}
	else if(btn4.Down()){
		window.SetTimeout(function(){
			if(!max){
				max = true;
				window.SetProperty('大封面', max);
				setSize(225,225);
			}else{
				max = false;
				window.SetProperty('大封面', max);
				setSize(280,100);
			}
		},50);
		btn4.Reset();
	}
	else if(btn5.Down()){
		var PboMenu = window.CreatePopupMenu();
		PboMenu.AppendMenuItem(0,1,'Default');
		PboMenu.AppendMenuItem(0,2,'Repeat (playlist)');
		PboMenu.AppendMenuItem(0,3,'Repeat (track)');
		PboMenu.AppendMenuItem(0,4,'Random');
		PboMenu.AppendMenuItem(0,5,'Shuffle (tracks)');
		PboMenu.AppendMenuItem(0,6,'Shuffle (albums)');
		PboMenu.AppendMenuItem(0,7,'Shuffle (folders)');
		PboMenu.CheckMenuRadioItem(1, 7, fb.PlaybackOrder+1);
		PboMenu.AppendMenuSeparator();
		PboMenu.AppendMenuItem(fb.StopAfterCurrent ? 8 : 0, 8,'Stop after current');
		var a = PboMenu.TrackPopupMenu(x,y);
		if(a >= 1 && a <= 7){
			fb.PlaybackOrder = a-1;
			max&&tooltip.Show(PBO[fb.PlaybackOrder]);
		}else if(a == 8){
			if(fb.StopAfterCurrent){
				fb.StopAfterCurrent = false;
			}else{
				fb.StopAfterCurrent = true;
			}
			max&&tooltip.Show('Stop after current');
		}
		PboMenu.Dispose();
		btn5.Reset();
	}
	else if(btn6.Down()){
		fb.RunMainMenuCommand("View/ESLyric/Show the Desktop Lyric");
		btn6.Reset();
	}
	else if(btn7.Down()){
		if(!volume.visable){
			volume.visable = true;
		}else{
			volume.visable = false;
			if(blurV){
				blurV.Dispose();
				blurV = null;
			}
		}
		window.RepaintRect(volume.bar.X - 10, volume.bar.Y - 5, volume.bar.W + 20, volume.bar.H + 10);
		btn7.Reset();
	}
	else if((fb.IsPlaying||fb.IsPaused)&&seek.Click(x,y)){
		setProgress(seek.Pos/seek.W);
		!max?seek.repaint():window.RepaintRect(seek.X - 35, seek.Y - 7, seek.W + 35, seek.H + 14);;
	}
	else if(volume.visable&&volume.bar.Click(x,y)){
		setVolume(volume.bar.Pos/volume.bar.H);
		volume.bar.repaint();
	}
	else if(x > 5&&x < ww - 5&&y > 5&&y < wh - 5){
		x_ = x;
		y_ = y;
		click = true;
	}else if(click){
		click = false;
	}
}

function on_mouse_lbtn_up(x,y){
	seek.Reset();
	volume.bar.Reset();
	if(click){
		_x = _y = x_ = y_ = 0;
		click = false;
	}
}

function on_mouse_lbtn_dblclk(x, y){
	if(!max&&x > bg_x&&y > bg_y&&x < (bg_x + bg_w)&&y < (bg_y + bg_h)){
		fb.RunMainMenuCommand("View/ESLyric/Show the Desktop Lyric");
	}
}

function on_mouse_rbtn_up(x,y){
	if(x > bg_x && y > bg_y && x < bg_x + bg_w && y < bg_y + bg_h){
		Menu(x,y);
		return true;
	}
}

function on_mouse_move(x,y){
	if(!move){
		move = true;
		window.Repaint();
	}
	btn0.Activate(x,y);
	btn1.Activate(x,y);
	btn2.Activate(x,y);
	btn3.Activate(x,y);
	btn4.Activate(x,y);
	btn5.Activate(x,y);
	if(max){
		btn6.Activate(x,y);
		btn7.Activate(x,y);
	}
	if((fb.IsPlaying||fb.IsPaused)&&seek.Drag(x,y)){
		setProgress(seek.Pos/seek.W);
		!max?seek.repaint():window.RepaintRect(seek.X - 35, seek.Y - 7, seek.W + 35, seek.H + 14);;
	}
	if(volume.visable&&volume.bar.Drag(x,y)){
		setVolume(volume.bar.Pos/volume.bar.H);
		volume.bar.repaint();
	}
	if(click){
		_x = x;
		_y = y;
		hWnd.Move(hWnd.Left + _x - x_, hWnd.Top + _y - y_, hWnd.Width, hWnd.Height, false);
	}
}

function on_mouse_leave(){
	if(move){
		window.SetTimeout(function(){
			move = false;
			if(blurT){
				blurT.Dispose();
				blurB.Dispose();
				blurV&&blurV.Dispose();
				blurT = blurB = blurV = null;
			}
			window.Repaint();
		},50);
	}
	btn0.Reset();
	btn1.Reset();
	btn2.Reset();
	btn3.Reset();
	btn4.Reset();
	btn5.Reset();
	btn6.Reset();
	btn7.Reset();
	if(click){
		_x = _y = x_ = y_ = 0;
		click = false;
	}
}

function on_paint(gr){
	gr.FillSolidRect(0,0,ww,wh,0xff000000);
	gr.SetSmoothingMode(2);
	shadow&&gr.DrawImage(shadow, 0, 0, ww, wh, 0, 0, ww, wh, 0, 255);
	bg_img&&gr.DrawImage(bg_img,5,5,ww-10,wh-10,0,0,bg_img.Width,bg_img.Height,0,255);
	g_img&&gr.DrawImage(g_img,bg_x,bg_y,bg_w,bg_h,0,0,g_img.Width,g_img.Height,0,Math.abs(g_fade));

	if(move){
		if(max){
			if(blurT == null){
				blurT = gdi.CloneGraphics(gr, 5, 5, ww - 10, 50);
				blurT.BoxBlur(5,2);
				blurB = gdi.CloneGraphics(gr, 5, wh - 60, ww - 10, 55);
				blurB.BoxBlur(5,2);
			}
			blurT&&gr.DrawImage(blurT, 5, 5, ww - 10, 50, 0, 0, ww - 10, 50, 0, Math.abs(g_fade));
			gr.SetSmoothingMode(0);
			gr.FillSolidRect(5, 5, ww - 10, 50, blurcolor);
			gr.DrawLine(5, 55, ww - 5, 55, 1, RGBA(0,0,0,32));
			gr.SetSmoothingMode(2);
			blurB&&gr.DrawImage(blurB, 5, wh - 60, ww - 10, 55, 0, 0, ww - 10, 55, 0, Math.abs(g_fade));
			gr.SetSmoothingMode(0);
			gr.FillSolidRect(5, wh - 60, ww - 10, 55, blurcolor);
			gr.DrawLine(5, wh - 60, ww - 5, wh - 60, 1, RGBA(0,0,0,32));
			gr.SetSmoothingMode(2);
			if(metadb){
				gr.GdiDrawText(title, g_font, 0xff000000, 45, 10, ww - 90, 20, DT_CV);
				gr.GdiDrawText(artist, g_font, 0xff000000, 45, 30, ww - 90, 20, DT_CV);
			}
			if(fb.PlaybackLength > 0){
				seek.Pos = parseInt(getProgress() * seek.W);
				gr.FillSolidRect(seek.X,seek.Y,seek.W,5,0x40000000);
				gr.FillSolidRect(seek.X,seek.Y+1,seek.Pos,3,0xffffffff);
				gr.DrawRect(seek.X - 1,seek.Y,seek.W + 1,5,1,0x50000000);
				gr.GdiDrawText(fb.TitleFormat("$if(%length%,[%playback_time%],'0:00')").Eval(true),gdi.Font('Segoe UI',11), 0xff000000, seek.X - 35, seek.Y - 7, 35, 20, DT_CV);
				gr.GdiDrawText(fb.TitleFormat("$if(%length%,%length%,'0:00')").Eval(true),gdi.Font('Segoe UI',11), 0xff000000, seek.X + seek.W, seek.Y - 7, 35, 20, DT_CV);
			}
			if(volume.visable){
				if(blurV == null){
					blurV = gdi.CloneGraphics(gr, volume.bar.X - 10, volume.bar.Y - 5, volume.bar.W + 20, volume.bar.H + 10);
					blurV.BoxBlur(5,2);
				}
				blurV&&gr.DrawImage(blurV,volume.bar.X - 10, volume.bar.Y - 5, volume.bar.W + 20, volume.bar.H + 10, 0, 0, blurV.Width, blurV.Height, 0, Math.abs(g_fade)); 
				gr.FillSolidRect(volume.bar.X - 10, volume.bar.Y - 5, volume.bar.W + 19, volume.bar.H + 9, blurcolor);
				gr.DrawRect(volume.bar.X - 10, volume.bar.Y - 5, volume.bar.W + 19, volume.bar.H + 9, 1, RGBA(0,0,0,32));
				gr.FillSolidRect(volume.bar.X,volume.bar.Y,5,volume.bar.H,0x40000000);
				gr.FillSolidRect(volume.bar.X+1,volume.bar.Y,3,volume.bar.Pos,0xffffffff);
				gr.DrawRect(volume.bar.X,volume.bar.Y - 1,5,volume.bar.H + 1,1,0x50000000);
			}
			btn6.Draw(gr);
			btn7.Draw(gr);
		}else{
			if(fb.PlaybackLength > 0){
				seek.Pos = parseInt(getProgress() * seek.W);
				gr.FillSolidRect(seek.X,seek.Y,seek.W,5,0x40000000);
				gr.FillSolidRect(seek.X,seek.Y+1,seek.Pos,3,0xffffffff);
				gr.DrawRect(seek.X - 1,seek.Y,seek.W + 1,5,1,0x50000000);
				gr.GdiDrawText(fb.TitleFormat("$if(%length%,%length%,'0:00')").Eval(true),gdi.Font('Segoe UI',11), 0xff000000, seek.X + seek.W, seek.Y - 7, 35, 20, DT_CV);
			}
		}

		btn0.Draw(gr);
		btn1.Draw(gr);
		btn2.Draw(gr);
		btn3.Draw(gr);
		btn4.Draw(gr);
		btn5.Draw(gr);
	}else{
		if(!max){
			if(metadb){
				gr.GdiDrawText(title, g_font, 0xff000000, 120, 15, ww - 135, 20, DT_LV);
				gr.GdiDrawText(artist, g_font, 0xff000000, 120, 40, ww - 135, 20, DT_LV);
			}
			if(fb.PlaybackLength > 0){
				seek.Pos = parseInt(getProgress() * seek.W);
				gr.FillSolidRect(seek.X,seek.Y,seek.W,5,0x40000000);
				gr.FillSolidRect(seek.X,seek.Y+1,seek.Pos,3,0xffffffff);
				gr.DrawRect(seek.X - 1,seek.Y,seek.W + 1,5,1,0x50000000);
				gr.GdiDrawText(fb.TitleFormat("$if(%length%,%length%,'0:00')").Eval(true),gdi.Font('Segoe UI',11), 0xff000000, seek.X + seek.W, seek.Y - 7, 35, 20, DT_CV);
			}
			btn3.Draw(gr);
			btn4.Draw(gr);
		}
	}
	tooltip.Paint(gr);
}

function on_notify_data(name, info){
	if(name != ThisPanelsName) return;
	if(info == 'MiniMode'){
		fb.RunMainMenuCommand("View/Flowin/Mini/Show");
	}
	if(info == 'download'){
		ImageAnime();
	}
	if(typeof(info) == "number"){
		blurcolor = info;
		ImageAnime();
	}
}    

function on_playback_time(time){
	if(move||!max){
		seek.Pos = parseInt(getProgress() * seek.W);
		!max?seek.repaint():window.RepaintRect(seek.X - 35, seek.Y - 7, seek.W + 35, seek.H + 14);
	}
}

function on_playback_order_changed(new_index){
	btn5.image.Dispose();
	btn5.image = gdi.Image(icoPath + 'Pbo\\' + new_index + '.png');
	btn5.repaint();
}

function on_volume_change(val){
	volume.bar.Pos = parseInt(getVolume() * volume.bar.H);
	switch(true){
	case volume.bar.Pos <= 0:
		btn7.image = gdi.Image(icoPath + 'Vol\\1.png');
		btn7.repaint();
		break;
	case volume.bar.Pos <= 30:
		btn7.image = gdi.Image(icoPath + 'Vol\\2.png');
		btn7.repaint();
		break;
	case volume.bar.Pos <= 60:
		btn7.image = gdi.Image(icoPath + 'Vol\\3.png');
		btn7.repaint();
		break;
	case volume.bar.Pos <= 100:
		btn7.image = gdi.Image(icoPath + 'Vol\\4.png');
		btn7.repaint();
		break;
	default: break; 
	}
	if(volume.visable){
		volume.bar.repaint();
		tooltip.Show(volume.bar.Pos+' %');
	}
}

function ImageAnime(){
	g_fade = 255;
	g_timer&&window.ClearInterval(g_timer);
	g_timer = window.SetInterval(function(){
		g_fade -= 51;
		if(blurT){
			blurT.Dispose();
			blurB.Dispose();
			blurV&&blurV.Dispose();
			blurT = blurB = blurV = null;
		}
		if(g_fade == 0){
			getImage();
		}
		if(g_fade <= -255){
			g_fade = 255;
			window.ClearInterval(g_timer);
			g_timer = null;
		}
		window.Repaint();
	},50);
}

function on_drag_drop(action, x, y){
	action.ToPlaylist();
}